home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / internet / other / ka9q / nhclb120.zoo / linux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  2.8 KB  |  142 lines

  1. /* Driver for 3COM Ethernet card */
  2.  
  3. #define    TIMER    20000    /* Timeout on transmissions */
  4.  
  5. #include <stdio.h>
  6. #include <linux/eth.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "enet.h"
  10. #include "iface.h"
  11. #include "timer.h"
  12. #include "arp.h"
  13. #include "trace.h"
  14. #include <errno.h>
  15. #include <fcntl.h>
  16.  
  17. unsigned char linux_hwaddr[6]=
  18. {
  19.   0x0,0x0,0x0,0x0,0x0,0x0
  20. };
  21. static unsigned char buff[2048];
  22. int
  23. linux_raw(struct interface *i, struct mbuf *bp)
  24. {
  25.   int size;
  26.   int count;
  27.   int res;
  28.   struct mbuf *b;
  29.   size =0;
  30.   b=bp;
  31.   while (b != NULLBUF)
  32.     {
  33.       memcpy(buff+size, b->data, b->cnt);
  34.       size+= b->cnt;
  35.       b=b->next;
  36.     }
  37.   /* try to write it 10 times.  It shouldn't take long for the
  38.      ethernet to finish sending a packet. */
  39.   for (count=0; count < 10; count++)
  40.     {
  41.       res = write(i->dev,buff,size);
  42.       if (res > 0) 
  43.     {
  44.       free_mbuf(bp);
  45.       return (0);
  46.     }
  47.       if (res != EAGAIN)
  48.     break;
  49.     }
  50.   free_p(bp);
  51.   return (-1);
  52.   
  53. }
  54.  
  55. int
  56. linux_stop(int dev)
  57. {
  58.   close(dev);
  59.   return (0);
  60. }
  61.  
  62. void
  63. linux_recv (struct interface *i)
  64. {
  65.   int len;
  66.   struct mbuf *bp;
  67.  
  68.   while (1)
  69.     {
  70.       len = read(i->dev,buff,2048);
  71.       if (len <= 0) return;
  72.       bp= (void *)malloc (sizeof *bp);
  73.       if (bp == NULL) return;
  74.       bp->data = malloc (len);
  75.       if (bp->data == NULL)
  76.     {
  77.       free(bp);
  78.       return;
  79.     }
  80.       memcpy (bp->data,buff,len);
  81.       bp->next = NULL;
  82.       bp->anext = NULL;
  83.       bp->size = len;
  84.       bp->cnt = len;
  85.       eproc (i,bp);
  86.     }
  87. }
  88.  
  89. linux_attach(argc,argv)
  90. int argc;
  91. char *argv[];
  92. {
  93.     register struct interface *if_linux;
  94.     extern struct interface *ifaces;
  95.     int enet_send();
  96.     int enet_output();
  97.     int pether(),gaether();
  98.     int i,dig[6];
  99.  
  100.     if (6 != sscanf(argv[4],"%x.%x.%x.%x.%x.%x", 
  101.                &dig[0], &dig[1], &dig[2], &dig[3], &dig[4], &dig[5])) {
  102.       printf("linux_attach: Ethernet address must 6 hex octets in format 1.2.3.4.5.6\n");
  103.       return -1;
  104.     }
  105.     for (i = 0; i < 6; i++)
  106.       linux_hwaddr[i] = dig[i];
  107.  
  108.     arp_init(ARP_ETHER,EADDR_LEN,IP_TYPE,ARP_TYPE,ether_bdcst,pether,gaether);
  109.     if((if_linux = (struct interface *)calloc(1,sizeof(struct interface))) == NULLIF
  110.      ||(if_linux->name = malloc((unsigned)strlen(argv[2])+1)) == NULLCHAR){
  111.         printf("linux_attach: no memory!\n");
  112.         return -1;
  113.     }
  114.     if_linux->hwaddr=linux_hwaddr;
  115.     strcpy(if_linux->name,argv[2]);
  116.     if_linux->mtu = atoi(argv[3]);
  117.     if_linux->send = enet_send;
  118.     if_linux->output = enet_output;
  119.     if_linux->raw = linux_raw;
  120.     if_linux->recv = linux_recv;
  121.     if_linux->stop = linux_stop;
  122.     if_linux->dev = open ("/dev/eth",O_RDWR|O_NDELAY);
  123.     if (if_linux->dev < 0)
  124.       {
  125.         perror ("/dev/eth");
  126.         return (-1);
  127.       }
  128.  
  129.     if(strcmp(argv[1],"arpa") != 0){
  130.         printf("Mode %s unknown for interface %s\n",
  131.             argv[1],argv[2]);
  132.         free(if_linux->name);
  133.         free((char *)if_linux);
  134.         return -1;
  135.     }
  136.  
  137.     if_linux->next = ifaces;
  138.     ifaces = if_linux;
  139.  
  140.     return 0;
  141. }
  142.